package com.ktjiaoyu.controller;

import com.ktjiaoyu.entity.SysUser;
import com.ktjiaoyu.ex.LoginException;
import com.ktjiaoyu.service.SysUserService;
import com.ktjiaoyu.utils.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;


@Controller // 表示当前类是一个控制器
public class LogionController extends BaseController{
    @Autowired // 表示自动注入
    private SysUserService sysUserService;
//    @Autowired
//    public HttpServletRequest request;
//    //负责转发到任意路劲
//    public String forward(String path){
//        return UrlBasedViewResolver.FORWARD_URL_PREFIX+path;
//    }
//    //负责重定向任意路劲
//    public String redirect(String path){
//        return UrlBasedViewResolver.REDIRECT_URL_PREFIX+path;
//    }
//    //获取request对象
//    public HttpServletRequest request(){
//        return request;
//    }
//    //获取session对象
//    public HttpSession session(){
//        return request().getSession();
//    }

    @GetMapping("/login")
    public String toLogin(){
        //由于login.jsp页面不在WEB-INF下,所以不能返回逻辑试图名称
        //需要使用forward:前缀告知SpringMVC框架，使用转发跳转到login.jsp页面
        return forward("/login.jsp");
    }

    @PostMapping("/login")
    public String login(@RequestParam("account")String account,
                        @RequestParam("password") String password) {

//        String url = "jdbc:mysql://localhost:3306/ktsms?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai";
//        String user = "root";
//        String password2 = "123456";
//        try (Connection conn = DriverManager.getConnection(url, user, password2)) {
//            System.out.println("连接成功！");
//        } catch (SQLException e) {
//            throw new CannotCreateTransactionException("JDBC连接数据库失败");
//        }



        SysUser loginUser=sysUserService.login(account,password);

        if (loginUser!=null){
            //登录成功,用户信息存入session中
//            session().setAttribute("loginUser",loginUser);
            session(Constants.SESSION_USER,loginUser);
            //重定向后台首页
//            return "redirect:/main/index";
            return redirect("/main/index");
        }else {
//            request().setAttribute("msg","账号或密码错误");
            //登录失败,返回登录页面
//            request(Constants.REQUEST_ERROR,"账号或密码错误");
            throw new LoginException("账号或密码错误");
//            return forward("/login.jsp");
        }
    }

    @GetMapping("/main/index")
    public String index(){
        return "index";
    }

    @GetMapping("/ex")
    public void throwException(){
        throw new RuntimeException("测试不同异常映射到不同的页面");

    }

//    @ExceptionHandler(value = RuntimeException.class)
//    public String handlerException(RuntimeException e,HttpServletRequest request){
//        request.setAttribute(Constants.REQUEST_ERROR,e);
//        return "500";
//    }
}